package com.qf.medicaldoctoradmin.illnesscare.collection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qf.common.entity.IllnessCare;
import com.qf.common.entity.IllnessCareCollect;
import com.qf.common.utils.DateUtils;
import com.qf.common.utils.RedisUtils;
import com.qf.common.utils.UserUtils;
import com.qf.medicaldoctoradmin.illnesscare.article.dao.IllnessCareDao;
import com.qf.medicaldoctoradmin.illnesscare.collection.dao.IllnessCareCollectDao;
import com.qf.medicaldoctoradmin.illnesscare.collection.service.IllnessCareCollectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class IllnessCareCollectServiceImpl implements IllnessCareCollectService {

    @Resource
    private IllnessCareCollectDao illnessCareCollectDao;

    @Resource
    private IllnessCareDao illnessCareDao;

    @Override
    public void collect(Integer careArticleId) {
        // 收藏文章
        // 获取用户id
        Integer uid = UserUtils.getUid();
        // 如果没有收藏进行收藏，否则取消收藏
        String collectKey = String.format("collect:%s",careArticleId);
        // 将当天收藏数存到redis中
        String todayCollect = String.format("collect:%s", DateUtils.formatDate(new Date(), "yyyyMMdd"));
        // 使用hashGet获取用户的id
        Integer v = (Integer) RedisUtils.hashGet(collectKey, uid.toString());
        if (v == null || v.equals(0)) {
            // 如果没有收藏，可以进行收藏
            RedisUtils.hashPut(collectKey, uid.toString(), 1);
            // 反复收藏的处理
            if (v != null) {
                illnessCareCollectDao.update(null,
                        new LambdaUpdateWrapper<IllnessCareCollect>()
                                .set(IllnessCareCollect::getCollectFlag,1)
                                .eq(IllnessCareCollect::getUserId,uid)
                                .eq(IllnessCareCollect::getCareArticleId,careArticleId));
            } else {
                IllnessCareCollect illnessCareCollect = new IllnessCareCollect();
                illnessCareCollect.setCareArticleId(careArticleId);
                illnessCareCollect.setUserId(uid);
                illnessCareCollect.setCollectFlag(1);
                illnessCareCollectDao.insert(illnessCareCollect);

            }
            // 收藏数加1
            RedisUtils.hashIncr(todayCollect,careArticleId.toString(),1);

        } else {
            // 已经收藏则取消收藏，将redis的值变为0
            RedisUtils.hashPut(collectKey,uid.toString(),0);
            // 数据库的状态设置为0
            illnessCareCollectDao.update(null,
                    new LambdaUpdateWrapper<IllnessCareCollect>()
                            .set(IllnessCareCollect::getCollectFlag,0)
                            .eq(IllnessCareCollect::getUserId,uid)
                            .eq(IllnessCareCollect::getCareArticleId,careArticleId));
            RedisUtils.hashIncr(todayCollect,careArticleId.toString(),-1);
        }
    }

    @Override
    public List<IllnessCare> selectById(Integer userId) {
        List<IllnessCareCollect> illnessCareCollect = illnessCareCollectDao.selectList(new LambdaQueryWrapper<IllnessCareCollect>()
                .eq(IllnessCareCollect::getUserId, UserUtils.getUid()));
        List<Integer> illFavorite = illnessCareCollect.stream()
                .map(IllnessCareCollect::getCareArticleId)
                .collect(Collectors.toList());
        List<IllnessCare> illnessCareCollects = new ArrayList<>();
        if (!illFavorite.isEmpty()) {
            illnessCareCollects = illnessCareDao.selectList(new LambdaQueryWrapper<IllnessCare>()
                    .in(IllnessCare::getId, illFavorite));
        }

        return illnessCareCollects;
    }

}
